#include "ask_request.h"

#include <iostream>
#include <sstream>
#include <string>
#include <map>

#include "base/logging.h"
#include "base/json_escape.h"

#include "auto_reply_cache.h"

namespace im {
namespace fcgi {

bool AskRequest::Response() {
  if (!fcgi_out_) {
    LOG_WARN("null fcgi_out!");
    return false;
  }

  std::string question = GetPostArg("q");
  if (question.empty() && !GetQuery("t").empty()) {
    question = GetQuery("q");
  }

  std::string answer = AutoReplyCache::Instance().GetReply(question);

  FCGX_PutS("Content-type: text/html; charset=utf-8\r\n\r\n", fcgi_out_);

  std::string rsp_body;
  rsp_body += "{\"code\":0, \"answer\":\"" + base::JsonEscape(answer) + "\"}";
  FCGX_PutS(rsp_body.c_str(), fcgi_out_);
  LOG_INFO("AskRequest q=" << question << " body.size=" << rsp_body.size());
  return true;
}

}
}


